package tree.generator;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import tree.person.Person;


/** Tree class generator */
public class MakeTree {

	/** constant man number */
	public final static int MAN = 0;

	/** constant woman number */
	public final static int WOMAN = 1;

	/** constant surname number */
	public final static int SURNAME = 10;

	/** constant name number */
	public final static int NAME = 20;

	/** path to image file */
	private static String foto[] = {"face.gif"};

	/** sex of person */
	private static String sex[] = {"M", "W"};

	/** man and woman surnames list */
	private static String surname[][] ={
		{"Piotr", "Jakub", "Szymon", "Kacper", "Andrzej", "Jan", "Filip", "Michal", "Mateusz", "Marcin"},
		{"Barbara", "Maryla", "Julia", "Wiktoria", "Oliwia", "Natalia", "Zofia", "Iwona", "Anna", "Ewa"}
	};

	/** popular names list */
	private static String name[] = {
		"Kowal", "Tlalka", "Niemen", "Kral", "Ruczaj", "Ryba", "Pratek", "Wasik", "Wojcik", "Nowak",
		"Wozniak", "Mazur", "Krawczyk", "Dudek", "Zajac", "Pawlak", "Szewczyk", "Wlodarczyk", "Sawicki", "Kubiak"
	};

	/** number of population */
	private long population;

	/** number of tree */
	private long treeNumber;

	/** random generator */
	private Random random;

	/** population Id */
	private long pNumber;

	/** person Id */
	private long id;

	/** person array list */
	List<Person> tree;


	/** default constructor */
	public MakeTree(long population, long treeNumber) {
		this.population = population;
		this.treeNumber = treeNumber;

		this.random = new Random();
		this.pNumber = 0;
		this.id = 0;

		this.tree = new ArrayList<Person>();
	}


	/** method build Persons list */
	public void start() {
		while (this.treeNumber > 0) {
			this.generate(0, 0, "Start");
			this.treeNumber--;
			this.pNumber = 0;
		}
	}


	/** method tree generator */
	private boolean generate(long mumId, long dadId, String pName) {

		/** sex random */
		int rSex = 0;
		/** name random */
		int rName = 0;
		/** child random */
		int rChild = 0;

		//sex:
		rSex = random.nextInt(2);

		//marriage:
		if(random.nextInt(10) > 2) {
			if (rSex == WOMAN) {
				rName = random.nextInt(NAME);
				pName = name[rName];

				mumId = addPerson(mumId, dadId, pName, WOMAN);
				dadId = addPerson(0, 0, pName, MAN);
			}
			else {
				dadId = addPerson(mumId, dadId, pName, MAN);
				mumId = addPerson(0, 0, pName, WOMAN);
			}

			//child:
			rChild = 1 + random.nextInt(6);

			//child list:
			while ((this.pNumber < this.population) && (rChild > 0)) {
				rChild--;
				generate(mumId, dadId, pName);
			}
		}
		else {
			addPerson(mumId, dadId, pName, rSex);
		}
		return true;
	}


	/** method add one person */
	private long addPerson(long mumId, long dadId, String pName, int rSex) {

		int rSurname;
		String pSurname;

		//id:
		this.id++;
		this.pNumber++;
		
		//sex:
		String pSex = sex[rSex];

		//surname:
		rSurname = random.nextInt(SURNAME);
		pSurname = surname[rSex][rSurname];

		//person:		
		this.tree.add(new Person(this.id, mumId, dadId, pName, pSurname, pSex, foto[0]));
		return this.id;
	}


	/** method get person list */
	public List<Person> getTree() {
		return this.tree;
	}
}










	